package com.ruoyi.web.controller.system.community;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.communiry.FzSCommunity;
import com.ruoyi.system.service.community.IFzSCommunityService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 小区Controller
 * 
 * @author ruoyi
 * @date 2020-02-26
 */
@Controller
@RequestMapping("/system/community")
public class FzSCommunityController extends BaseController
{
    private String prefix = "system/community";

    @Autowired
    private IFzSCommunityService fzSCommunityService;

    @RequiresPermissions("system:community:view")
    @GetMapping()
    public String community()
    {
        return prefix + "/community";
    }

    /**
     * 查询小区列表
     */
    @RequiresPermissions("system:community:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FzSCommunity fzSCommunity)
    {
        startPage();
        List<FzSCommunity> list = fzSCommunityService.selectFzSCommunityList(fzSCommunity);

        return getDataTable(list);
    }

    /**
     * 导出小区列表
     */
    @RequiresPermissions("system:community:export")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzSCommunity fzSCommunity)
    {
        List<FzSCommunity> list = fzSCommunityService.selectFzSCommunityList(fzSCommunity);
        ExcelUtil<FzSCommunity> util = new ExcelUtil<FzSCommunity>(FzSCommunity.class);
        return util.exportExcel(list, "community");
    }

    /**
     * 新增小区
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";

    }

    /**
     * 新增保存小区
     */
    @RequiresPermissions("system:community:add")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FzSCommunity fzSCommunity)
    {
        fzSCommunity.setCommunityId(HeadNum.COMMUNITY_HEAD+ DateUtils.getTimeMillis());
        SysUser user = ShiroUtils.getSysUser();
        fzSCommunity.setCreateBy(user.getUserName());
        return toAjax(fzSCommunityService.insertFzSCommunity(fzSCommunity));
    }

    /**
     * 修改小区
     */
    @GetMapping("/edit/{communityId}")
    public String edit(@PathVariable("communityId") String communityId, ModelMap mmap)
    {
        FzSCommunity fzSCommunity = fzSCommunityService.selectFzSCommunityById(communityId);
        mmap.put("fzSCommunity", fzSCommunity);
        return prefix + "/edit";
    }

    /**
     * 修改保存小区
     */
    @RequiresPermissions("system:community:edit")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzSCommunity fzSCommunity)
    {
        SysUser user = ShiroUtils.getSysUser();
        fzSCommunity.setUpdateBy(user.getUserName());
        return toAjax(fzSCommunityService.updateFzSCommunity(fzSCommunity));
    }

    /**
     * 删除小区
     */
    @RequiresPermissions("system:community:remove")
    @Log(title = "小区", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzSCommunityService.deleteFzSCommunityByIds(ids));
    }


    @GetMapping("/positionCommunity")
    @ResponseBody
    public AjaxResult updatePosition(HttpSession session,@Param("communityid") String communityid)
    { Subject subject = SecurityUtils.getSubject();
        List<FzSCommunity> fzSCommunities=(List<FzSCommunity>)session.getAttribute("community");
        FzSCommunity fzSCommunity=(FzSCommunity)session.getAttribute("ycommunity");
        if (fzSCommunity.getCommunityId().equals(communityid)){
            return toAjax(true);
        }
        fzSCommunities.forEach(
                fzSCommunity1 -> {
                    if (fzSCommunity1.getCommunityId().equals(communityid)){session.setAttribute("ycommunity",fzSCommunity1);}
                }
        );
        return toAjax(true);
    }
    @GetMapping("/editDian")
    public String goeditDian(ModelMap map, @RequestParam(name="along",required = false ) String yi, @RequestParam(name="latitude",required = false) String er)
    {
        map.put("yi",yi);
        map.put("er",er);
        return prefix + "/dian";
    }
    @GetMapping("/editAdd")
    public String goeditAdd(ModelMap map, @RequestParam(name="along",required = false ) String yi, @RequestParam(name="latitude",required = false) String er)
    {
        map.put("yi",yi);
        map.put("er",er);
        return prefix + "/dianAdd";
    }
}
